
import { _decorator, Component, Node, Vec3, Layers, find, director } from 'cc';
import { BundlesConst } from '../ccc3-framework-core/const/Const';
import cameraManager from '../ccc3-framework-core/framework/extends/cameraManager';
import dataUtil from '../ccc3-framework-core/framework/modules/dataUtil';
import { entityUtil } from '../ccc3-framework-core/framework/modules/entityUtil';
import eventUtil from '../ccc3-framework-core/framework/modules/eventUtil';
import { uiManager } from '../ccc3-framework-core/framework/modules/uiManager';
import { gameState } from '../ccc3-framework-core/gameState';
import { CoordinateTool } from '../ccc3-framework-core/utils/TransformUtil';
import { entityDefine, eventDefine, guideStep } from './CustomDefine';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = homeUtil
 * DateTime = Wed Jun 01 2022 15:47:34 GMT+0800 (中国标准时间)
 * Author = laozhou
 * FileBasename = fingerUtil.ts
 * FileBasenameNoExtension = fingerUtil
 * URL = db://assets/subs/form/fingerUtil.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('fingerUtil')
export class fingerUtil {

    constructor() {

    }

    private static handNode: Node = null;
    private static followNode: Node = null;

    public static hasFinger() {
        return this.handNode != null;
    }

    /**
     * 显示引导的手指 UI_2D 层
     * @param position 
    */
    public static showFinger(position: Vec3, followOffset: Vec3 = new Vec3(0, 0, 0)) {
        this.removeFollow();
        this.removeFinger();
        this.followOffset = followOffset;
        this.createFinger(entityDefine.guideFinger, position);
    }

    private static createFinger(fingerName: string, position: Vec3) {
        if (this.handNode == null) {
            entityUtil.showEntity(fingerName, uiManager.uiRoot, (handNode) => {
                this.handNode = handNode;
                this.handNode.setWorldPosition(position);
                this.handNode.setSiblingIndex(uiManager.uiRoot.children.length - 1)
            }, BundlesConst.common);

        }
        else if (this.handNode.parent != null) {
            this.handNode.setWorldPosition(position);
            this.handNode.setSiblingIndex(uiManager.uiRoot.children.length - 1)
        }
    }

    private static followInterval: number = null;
    public static followUI(followNode: Node) {
        this.followNode = followNode;
        this.followInterval = setInterval(() => {
            if (this.handNode != null && this.handNode.parent != null && this.followNode != null) {
                this.handNode.setWorldPosition(Vec3.add(this.tempPos, followNode.worldPosition, this.followOffset));
                this.handNode.setSiblingIndex(uiManager.uiRoot.children.length - 1)
            }
        }, 20);
    }
    private static tempPos: Vec3 = new Vec3(0, 0, 0);
    private static followOffset: Vec3 = new Vec3(0, 0, 0);
    public static follow3D(followNode: Node) {
        this.removeFollow();
        this.followNode = followNode;
        this.followInterval = setInterval(() => {
            if (gameState.instance.townUI && gameState.instance.townCamera) {
                let uiPos = CoordinateTool.transWorldPositionToUI(followNode.worldPosition, gameState.instance.townUI, gameState.instance.townCamera);
                if (this.handNode)
                    this.handNode.position = Vec3.add(this.tempPos, uiPos, this.followOffset);
            }
        }, 20);
    }


    public static removeFollow() {
        if (this.followInterval != null) {
            clearInterval(this.followInterval);
            this.followInterval = null;
        }
    }

    public static removeFinger() {
        if (this.handNode != null) {
            entityUtil.hideEntity(this.handNode)
            this.handNode = null;
        }
    }


    public static showCoinGuide(coinNode: Node) {
        let currentScene = director.getScene();
        let cm = currentScene.getComponentInChildren(cameraManager);
        if (cm && cm.isVisibleOn3dCamera(coinNode.getWorldPosition())) {
            if (!dataUtil.testStep(guideStep.receiveCoin)) {
                fingerUtil.showFinger(coinNode.getWorldPosition());
                fingerUtil.follow3D(coinNode);
                eventUtil.dispatchEvent(eventDefine.SHOW_DIALOG, { step: guideStep.receiveCoin });
            };
            return true;
        }
        return false;
    }
}
/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
